package com.xiansi.controller;

import com.github.pagehelper.PageInfo;
import com.xiansi.pojo.Team;
import com.xiansi.service.TeamService;
import com.xiansi.vo.QueryTeamVO;
import com.xiansi.vo.ResultVO;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("team")
@ResponseBody
public class TeamController {
    @Resource
    private TeamService teamService;

    //如果实体类中的日期类型需要从页面获取数据，避免null转换为date类型出问题
    //解决方案一：@InitBinder
    //解决方案二：实体类的日期类型属性上添加注解@DateTimeFormat(pattern="yyyy-MM-dd")
    @InitBinder
    protected void initDateformatBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
    
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Team> queryByPage(Integer pageNum, Integer pageSize, QueryTeamVO vo) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Team> teamPageInfo = teamService.queryByPage(pageNum, pageSize, vo);
        return new ResultVO<>(teamPageInfo);
    }

    //添加一个球队
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Team> addTeam(Team team) {
        int i = teamService.addTeam(team);
        if (i == 1) {
            return new ResultVO<Team>();
        }
        return new ResultVO<Team>(500, "请稍后再试");
    }

    //根据主键查询球队
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Team> queryById(@PathVariable("id") Integer id) {
        Team team = teamService.queryById(id);
        return new ResultVO<Team>(team);
    }

    //根据主键id更新
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO<Team> updateTeam(@PathVariable("id") Integer teamId, Team team) {

        team.setTeamId(teamId);
        int i = teamService.updateTeam(team);
        if (i == 1) {
            return new ResultVO<Team>();
        }
        return new ResultVO<Team>(500, "更新失败");
    }

    //根据主键删除
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<Team> deleteTeam(@PathVariable("id") Integer teamId) {
        int i = teamService.deleteTeam(teamId);
        if (i == 1) {
            return new ResultVO<Team>();
        }
        return new ResultVO<Team>(500, "更新失败");
    }

    //文件上传
    @RequestMapping(value = "{id}", method = RequestMethod.POST)
    public ResultVO<Team> uploadTeam(@RequestParam("logo") MultipartFile myFile, HttpServletRequest request, @PathVariable("id") Integer teamId) {
        //文件上传到指定服务器位置
        String path = request.getServletContext().getRealPath("/img/uploadFile/");
        //获取原始文件的名称
        String originalFilename = myFile.getOriginalFilename();
        //定义文件的新名称
        String randomName = UUID.randomUUID().toString().replace("-", "");
        int i = originalFilename.lastIndexOf(".");
        String hz = originalFilename.substring(i);
        String logoName = randomName + hz;
        int num = 0;
        try {
            myFile.transferTo(new File(path + "/" + logoName));
            Team team = new Team();
            team.setTeamId(teamId);
            team.setTeamLogo(logoName);
            num = teamService.updateTeam(team);
            if (num == 1) {
                return new ResultVO<Team>();
            } else {
                return new ResultVO<Team>(500, "更新失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO<Team>(500, "图片上传异常" + e.getMessage());
        }
    }

}
